<?php
class PropertyTypesController extends AppController {
    var $name = 'PropertyTypes';

    function index() {
        $this->PropertyType->recursive = 0;
        $this->set('propertyTypes', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid property type', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('propertyType', $this->PropertyType->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->PropertyType->create();
            // ACA SETEO EL CREATED_BY y el UPDATED_BY
            $count = $this->PropertyType->query("SELECT count(property_type_order) as count FROM property_types where building_id = " . $this->data['PropertyType']['building_id'] .";");
	    if ($count[0][0]['count'] == 0) {
                 $this->data['PropertyType']['property_type_order'] = 1;
            } else {
                $next_value = $this->PropertyType->query("SELECT max(property_type_order) + 1 as nextval FROM property_types where building_id = " . $this->data['PropertyType']['building_id'] .";");
                $this->data['PropertyType']['property_type_order'] = $next_value[0][0]['nextval'];
            }
            $this->data['PropertyType']['end_date'] = null;
            if ($this->PropertyType->save($this->data)) {
                $this->Session->setFlash(__('The property type has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The property type could not be saved. Please, try again.', true));
            }
        }
        $buildings = $this->PropertyType->Building->find('list');
        $this->set(compact('buildings'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid property type', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            // ACA SETEO EL UPDATED_BY
            if ($this->PropertyType->save($this->data)) {
                $this->Session->setFlash(__('The property type has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The property type could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->PropertyType->read(null, $id);
        }
        $buildings = $this->PropertyType->Building->find('list');
        $this->set(compact('buildings'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for property type', true));
            $this->redirect(array('action'=>'index'));
        }
        if (empty($this->data)) {
            $this->data = $this->PropertyType->read(null, $id);
        }
        if (!empty($this->data)) {
            // ACA SETEO EL UPDATED_BY
            $this->data['PropertyType']['end_date'] = date('Y-m-d H:i:s');
            if ($this->PropertyType->save($this->data)) {
                $this->Session->setFlash(__('Property type deleted', true));
            } else {
                $this->Session->setFlash(__('Property type was not deleted', true));
            }
        }
        $this->redirect(array('action' => 'index'));
    }
}
?>